package work.thread_eg.concurrent.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock可以指定为公平锁  默认非公平锁
 * 公平锁指的是谁等在前面谁先执行 线程是在一个等待队列里的 锁的公平性不能保证线程调度的公平性
 * synchronized是非公平锁
 *
 */
public class ReentrantLockTest02 extends Thread{
    private static ReentrantLock lock = new ReentrantLock(true);

    @Override
    public void run() {
        for(int i= 0 ; i<100; i++) {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+ "获得锁");
            }finally {
                lock.unlock();
            }
        }
    }


    public static void main(String[] args) {
        ReentrantLockTest02 t = new ReentrantLockTest02();
        Thread thread1 = new Thread(t);
        Thread thread2 = new Thread(t);
        thread1.start();
        thread2.start();

    }
}
